{template 'common/header'}
<link rel="stylesheet" href="../addons/ewei_shop/template/mobile/zbh/static/css/mui.dtpicker.css" />
<link rel="stylesheet" href="../addons/ewei_shop/template/mobile/zbh/static/css/mui.picker.css" />
<link rel="stylesheet" href="../addons/ewei_shop/template/mobile/zbh/static/css/mui.poppicker.css" />
<div id='container'></div>

<script id='address_list' type='text/html'>
		<div class="mui-content content-bg-w7">
			<div class="list-address">
				<ul class="list-group-address">
					<%each list as value%>
					<li class="list-item-address address_list" data-addressid="<%value.id%>">
						<div class="top-address-info">
							<div class="info-name-phone">
								<span class="info-people-name"><%value.realname%></span>
								<span class="info-phone-num"><%value.mobile%></span>
							</div>
							<div class="info-address-detail">
								<span class="province"><%value.province%></span>
								<span class="city"><%value.city%></span>
								<span class="area"><%value.area%></span>
								<span class="address"><%value.address%></span>
							</div>
						</div>
						<div class="bottom-address-option">
							<div class="default-address-option">
								<div class="checkbox-member-edit">
									<input type="radio" name="sex_btn_man" value=""/>
									<span class="chexk-btn-change <%if value.isdefault=='1'%>active<%/if%>"></span>
									<span class="text-checkbox">默认地址</span>
								</div>
							</div>
							<div class="btn-edit-address">
								<span class="edit mui-icon mui-icon-compose"></span>
								<p class="">编辑</p>
							</div>
							<div class="btn-delete-address">
								<span class="remove mui-icon mui-icon-trash"></span>
								<p>删除</p>
							</div>
						</div>
					</li>
					<%/each%>
				</ul>
			</div>
			<a class="button-fixed-add-address" id="new_address">
				添加新地址
			</a>
		</div>
</script>
<script id='address_data' type='text/html'>
	<div class="mui-content content-bg-w7">
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left header-back-icon"></a>
			<h1 class="mui-title"><%if !address.id%>添加收货地址<%else%>编辑收货地址<%/if%></h1>
			<button class="address_sub1 mui-btn mui-btn-blue mui-btn-link mui-pull-right">保存</button>
		</header>
		<div class="list-member-info-edit">
				<input type='hidden' id='addressid' value="<%address.id%>"/>
				<ul class="list-group-member-info">
					<li class="list-item-member-info">
						<div class="text-menmber-tittle">
							<span>收</span>
							<span>货</span>
							<span>人</span>
						</div>
						<div class="input-member-edit">
							<input id="realname" type="text" placeholder="请输入您的姓名" value="<%if address.realname%><%address.realname%><%/if%>" />
						</div>
					</li>
					<li class="list-item-member-info">
						<div class="text-menmber-tittle">
							<span>联</span>
							<span>系</span>
							<span>电</span>
							<span>话</span>
						</div>
						<div class="input-member-edit">
							<input id="mobile" type="text" placeholder="请输入您的手机号码" value="<%if address.mobile%><%address.mobile%><%/if%>" />
						</div>
					</li>
					<li class="list-item-member-info">
						<div class="text-menmber-tittle">
							<span>所</span>
							<span>在</span>
							<span>地</span>
							<span>区</span>
						</div>
						<div class="input-member-edit">
							<input id="member_location_city" type="text" placeholder="请输入您所在的城市" value="<%if address.address%><%address.province%> <%address.city%> <%address.area%><%/if%>" />
						</div>
					</li>
					<li class="list-item-member-info">
						<textarea class="textarea-add-new-add" id="address" name="" rows="" cols="" placeholder="详细地址"><%if address.address%><%address.address%><%/if%></textarea>
					</li>
				</ul>
		</div>
	</div>
</script>
<script src="../addons/ewei_shop/template/mobile/zbh/static/js/mui.picker.js"></script>
<script src="../addons/ewei_shop/template/mobile/zbh/static/js/mui.dtpicker.js"></script>
<script src="../addons/ewei_shop/template/mobile/zbh/static/js/mui.poppicker.js"></script>
<script src="../addons/ewei_shop/template/mobile/zbh/static/js/city.data-3.js"></script>

<script language="javascript">
	require(['tpl', 'core'], function(tpl, core) {

		function bindEditEvents(){

			(function($, doc) {
				$.init();
				$.ready(function() {
					//普通示例

					//-----------------------------------------
					//					//级联示例
					var cityPicker3 = new $.PopPicker({
						layer: 3
					});
					cityPicker3.setData(cityData3);
					var showCityPickerButton = doc.getElementById('member_location_city');
					showCityPickerButton.addEventListener('tap', function(event) {
						cityPicker3.show(function(items) {
							showCityPickerButton.value = (items[0] || {}).text + " " + (items[1] || {}).text + " " + (items[2] || {}).text;
							//返回 false 可以阻止选择框的关闭
							//return false;
						});
					}, false);
				});
			})(mui, document);

			$('.address_sub1').click(function(){

				if($(this).attr('saving')=='1'){
					return;
				}
				if($('#realname').isEmpty()){
					mui.toast('请输入收货人!');
					return;
				}
				if(!$('#mobile').isMobile()){
					mui.toast('请输入正确的联系电话!');
					return;
				}
				if($('#member_location_city').isEmpty()){
					mui.toast('请输入您所在的城市!');
					return;
				}

				if($('#address').isEmpty()){
					mui.toast('请输入详细地址!');
					return;
				}

				$('.address_sub1').html('正在处理...').attr('saving',1);
				var member_location_city = $('#member_location_city').val().split(' ');
				core.json('shop/address',{
					op:'submit',
					id:$('#addressid').val(),
					addressdata: {
						realname: $('#realname').val(),
						mobile: $('#mobile').val(),
						address: $('#address').val(),
						province: $('#member_location_city').val().length>0?member_location_city[0]:0,
						city: $('#member_location_city').val().length>0?member_location_city[1]:0,
						area: $('#member_location_city').val().length>0?member_location_city[2]:0
					}
				},function(json){
					if(json.status==1){
						mui.toast('保存成功!');
						list();
					}else{
						$('.address_sub1').html('确认').removeAttr('saving');
						mui.toast('保存失败');
					}
				},true,true);
			});

		}
		function list(){
			core.json('shop/address',{},function(json){
				$('#container').html(  tpl('address_list',json.result) );

				$('.edit').click(function(){

					var id =$(this).closest('.address_list').data('addressid');
					console.log(id);
					core.json('shop/address',{op:'get',id:id},function(json){
						$('#container').html(  tpl('address_data',json.result) );
						console.log(json.result);
						bindEditEvents();
					},true);

				});

				$('.checkbox-member-edit').click(function(){
					var id =$(this).closest('.address_list').data('addressid');
					console.log(id);
					$(this).find('input').attr("checked", 'checked');
					$(this).find('.chexk-btn-change').addClass('active');
					$(this).parents('.list-item-address').siblings().find('.chexk-btn-change').removeClass('active');
					$(this).parents('.list-item-address').siblings().find('input').attr("checked", false);
					core.json('shop/address',{op:'setdefault',id:id},function(json){
						if(json.status==1){
							mui.toast('设置默认地址成功');
						}
						else{
							mui.toast('设置默认地址失败');
						}
					},true,true);
				});

				$('.remove').click(function(){
					var id =$(this).closest('.address_list').data('addressid');
					mui.confirm('确认要删除此地址?', '温馨提示',function(e){
					if(e.index == 1){
						var aobj = $('.address_list[data-addressid='+id +']');
						aobj.fadeOut(500,function(){
							aobj.remove();
						});
						core.json('shop/address',{op:'remove',id:id},function(json){
							if(json.status==1){
								if(json.result && json.result.defaultid){
									$('.ico i').removeClass('fa-check-circle-o').addClass('fa-circle-o').css('color','#999');
									$('.address_list[data-addressid='+json.result.defaultid +'] .ico i').removeClass('fa-circle-o').addClass('fa-check-circle-o').css('color','#0c9');
								}
								mui.toast('删除成功');
							}
							else{
								mui.toast('删除失败');
							}
						},true,true);
					}
					})
				});

				$('#new_address').click(function(){
					core.json('shop/address',{op:'new'},function(json){
						var result = json.result;
						$('#container').html(  tpl('address_data',result) );
						{if $trade['shareaddress']=='1'}
						var shareAddress = {php echo json_encode($shareAddress)};
						WeixinJSBridge.invoke('editAddress',shareAddress,function(res){
							if(res.err_msg=='edit_address:ok'){
								$("#realname").val( res.userName  );
								$('#mobile').val( res.telNumber );
								$('#address').val( res.addressDetailInfo );
								$('#member_location_city').val( res.proviceFirstStageName+' '+res.addressCitySecondStageName+' '+res.addressCountiesThirdStageName );
							}
						});
						{/if}
						bindEditEvents();
					},true);
				});
			},true);
		}

		list();

	})

</script>
{template 'common/footer'}